//
//  XDMyCustomerInvestmentViewController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/21.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDMyCustomerInvestmentViewController.h"
#import "AFHTTPSessionManager+POST.h"
#import "XDMyCustomerInvestmentcell.h"

@interface XDMyCustomerInvestmentViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong) NSArray *customerArr;
@end

@implementation XDMyCustomerInvestmentViewController

- (NSArray *)customerArr {
    if (_customerArr == nil) {
        _customerArr = [NSArray array];
    }
    return _customerArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"客户投资信息";
    [self setupUI];
    [self loadData];
}

- (void)setupUI {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.tableView.backgroundColor = kColorRGB(234, 234, 234);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    tableView.tableFooterView = view;
}


- (void)loadData {
    
    NSDictionary * param = @{@"method":@"customerInvestment",@"investId": [NSString stringWithFormat:@"%zd",self.investId]};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            
            NSDictionary *dic = responseObject[@"result"];
            /*
             result =     {
             ctime = "2016-08-01 10:49:30";
             inTime = "2017-02-07 19:06:19";
             limitTime = 3;
             limitType = "\U6708";
             mobile = 15801038440;
             name = "\U7eba\U7ec7\U5382\U751f\U610f\U5468\U8f6c5\U671f";
             payBackTime = "2017-05-08 19:06:19";
             postNumber = FC170126001;
             realMoney = 100;
             realName = "\U738b\U4f73\U742a";
             yMoney = "24.38";
             };
             type = success;
         
             */
//
            NSMutableString *mobile = [[NSMutableString alloc] initWithString: dic[@"mobile"]];
            [mobile insertString:@" " atIndex:3];
            [mobile insertString:@" " atIndex:8];
            
            
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
           
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            dateFormatter.dateFormat = @"yyyy-MM-dd";
            
            NSString *ctime = [dateFormatter stringFromDate:[fmt dateFromString:dic[@"ctime"]]];
            
            NSString *inTime = [dateFormatter stringFromDate:[fmt dateFromString:dic[@"inTime"]]];
            
            NSString *payBackTime = [dateFormatter stringFromDate:[fmt dateFromString:dic[@"payBackTime"]]];
            
            NSString *realMoneyStr = [NSString stringWithFormat:@"%.2f",[dic[@"realMoney"] doubleValue]];
            NSString *yMoneyStr = [NSString stringWithFormat:@"%.2f",[dic[@"yMoney"] doubleValue]];
            NSString *limitStr;
            if ([dic[@"limitType"] isEqualToString:@"月"]) {
                limitStr = [NSString stringWithFormat:@"%zd个%@", [dic[@"limitTime"] integerValue], dic[@"limitType"]];
            } else {
                limitStr = [NSString stringWithFormat:@"%zd%@", [dic[@"limitTime"] integerValue], dic[@"limitType"]];
            }
            
            self.customerArr = @[
  @[@"客户姓名",dic[@"realName"]],
  @[@"手机号",mobile],
  @[@"注册时间",ctime],
  @[@"投资时间",inTime],
  @[@"到期时间",payBackTime],
  @[@"项目名称",dic[@"name"]],
  @[@"封闭期限",limitStr],
  @[@"实付投资",realMoneyStr],
  @[@"年化业绩",yMoneyStr],
                                 ];
            
            
            [self.tableView reloadData];
            
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
            
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.customerArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *myID = @"XDMyAchieveTableViewCell";
    XDMyCustomerInvestmentcell *cell = [tableView dequeueReusableCellWithIdentifier:myID];
    if (cell == nil) {
        
        cell = [[XDMyCustomerInvestmentcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellWithTitle:self.customerArr[indexPath.row][0] dateil:self.customerArr[indexPath.row][1]];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 10)];
    view.backgroundColor = kColorRGB(234, 234, 234);
    return view;
}

@end
